package com.biostime.dingdingbox.modules.db.web;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.biostime.dingdingbox.common.config.Global;
import com.biostime.dingdingbox.common.persistence.Page;
import com.biostime.dingdingbox.common.utils.StringUtils;
import com.biostime.dingdingbox.common.web.BaseController;
import com.biostime.dingdingbox.modules.db.entity.Box;
import com.biostime.dingdingbox.modules.db.entity.Video;
import com.biostime.dingdingbox.modules.db.service.BoxService;
import com.biostime.dingdingbox.modules.db.service.VideoService;

/**
 * 视频表Controller
 * @author chenweicheng
 * @version 2016-07-01
 */
@Controller
@RequestMapping(value = "${adminPath}/db/video")
public class VideoController extends BaseController {

	@Autowired
	private VideoService videoService;
	
	@Autowired
	private BoxService boxService;
	
	@ModelAttribute
	public Video get(@RequestParam(required=false) String id) {
		Video entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = videoService.get(id);
		}
		if (entity == null){
			entity = new Video();
		}
		return entity;
	}
	
	@RequiresPermissions("db:video:view")
	@RequestMapping(value = {"list", ""})
	public String list(Video video, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<Video> page = videoService.findPage(new Page<Video>(request, response), video); 
		model.addAttribute("page", page);
		return "modules/db/videoList";
	}

	@RequiresPermissions("db:video:view")
	@RequestMapping(value = "form")
	public String form(Video video, Model model) {
		List<Box>  boxList = boxService.findList(new Box());		
		model.addAttribute("video", video);
		model.addAttribute("boxList", boxList);
		return "modules/db/videoForm";
	}

	@RequiresPermissions("db:video:edit")
	@RequestMapping(value = "save")
	public String save(Video video, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, video)){
			return form(video, model);
		}
		videoService.save(video);
		addMessage(redirectAttributes, "保存成功");
		return "redirect:"+Global.getAdminPath()+"/db/video/?repage";
	}
	
	@RequiresPermissions("db:video:edit")
	@RequestMapping(value = "delete")
	public String delete(@RequestParam(value="ids",required=false) String idsRequest, Video video, RedirectAttributes redirectAttributes) {
		//批量删除
		if(!StringUtils.isEmpty(idsRequest)){
			String[] ids = idsRequest.split(",");
			for(String id : ids){
				videoService.delete(new Video(id));
			}
		}else{
			videoService.delete(video);
		}
		
		addMessage(redirectAttributes, "删除成功");
		return "redirect:"+Global.getAdminPath()+"/db/video/?repage";
	}

}